#include "../../framework/framework.h"
#include "../inc/xPict.h"
#include "../inc/xDevice.h"
#include "../inc/xMedia.h"
namespace Sky
{
	TInt xPict::ms_Width = D3DX_DEFAULT_NONPOW2;
	TInt xPict::ms_Height = D3DX_DEFAULT_NONPOW2;
	TInt xPict::ms_Depth = D3DX_DEFAULT;
	TUint xPict::ms_MipLevels = D3DX_DEFAULT;
	TUint xPict::ms_Usage = D3DUSAGE_DYNAMIC;
	D3DFORMAT xPict::ms_Format = D3DFMT_UNKNOWN;
	D3DPOOL xPict::ms_Pool = D3DPOOL_DEFAULT;
	TUint xPict::ms_Filter = D3DX_DEFAULT;
	TUint xPict::ms_MipFilter  = D3DX_DEFAULT;

	D3DCOLOR xPict::ms_ColorKey = 0;
	TVoid xPict::Initialise()
	{
		m_Type = Type_Normal;
		m_NormaTexture = vNull;
		m_CubeTexture = vNull;
		m_VolumeTexture = vNull;
		m_Res = vNull;

		m_Width = ms_Width;
		m_Height = ms_Height;
		m_Depth = ms_Depth;
		m_Filter = ms_Filter;
		m_MipLevels = ms_MipLevels;
		m_Usage = ms_Usage;
		m_Format = ms_Format;
		m_Pool = ms_Pool;;
		m_ColorKey = ms_ColorKey;
	}

	TVoid xPict::Finalise()
	{
		SAFE_RELEASE(m_Res);
		m_VolumeTexture = vNull;
		m_CubeTexture = vNull;
		m_NormaTexture = vNull;
	}

	xPict::xPict()
	{
		Initialise();
	}

	xPict::~xPict()
	{
		Finalise();
	}
	
	TResult xPict::onCreate( xDxApp* app )
	{
		return XE_OK;
	}

	TResult xPict::onDelete( xDxApp* app )
	{
		Finalise();
		return XE_OK;
	}

	TResult xPict::onCreate( xDevice* device )
	{
		TResult hr = XE_APICALL;
		D3DXIMAGE_INFO info;
		if(m_Res != vNull){
			onDelete(device);
		}
		TChar fullName[512] = {0};
		switch(m_Type)
		{
		case Type_Normal|From_Create:
			hr = D3DXCreateTexture(device->getDevice9(),m_Width,m_Height,m_MipLevels,m_Usage,m_Format,m_Pool,&m_NormaTexture);
			break;
		case Type_Normal|From_File:
			//myDeV_Ret_hr_M(xMedia::FindMediaFile(xMedia::eType_image,m_srcFile.GetStr(),fullName,sizeof(fullName)),XE_APICALL);
			//hr = D3DXCreateTextureFromFile(device->getDevice9(),fullName,&m_NormaTexture);
			//break;
		case Type_Normal|From_File | From_Ex:
			myDeV_Ret_hr_M(xMedia::FindMediaFile(xMedia::eType_image,m_srcFile->toCharString(),fullName,sizeof(fullName)),XE_APICALL);
			hr = D3DXCreateTextureFromFileEx(device->getDevice9(),fullName,
				m_Width,m_Height,m_MipLevels,m_Usage,m_Format,m_Pool,m_Filter,m_MipFilter,m_ColorKey,&info,vNull,&m_NormaTexture);
			if(SUCCEEDED(hr))
			{
				m_Width = info.Width;
				m_Height = info.Height;
			}
			break;
		case Type_Normal|From_Mem:
			hr = D3DXCreateTextureFromFileInMemory(device->getDevice9(),m_srcData,m_srcSize,&m_NormaTexture);
			break;
		case Type_Normal|From_Mem | From_Ex:
			hr = D3DXCreateTextureFromFileInMemoryEx(device->getDevice9(),m_srcData,m_srcSize,
				m_Width,m_Height,m_MipLevels,m_Usage,m_Format,m_Pool,m_Filter,m_MipFilter,m_ColorKey,&m_srcInfo,
				vNull,&m_NormaTexture);
			break;
		case Type_Normal | From_Module:
		case Type_Normal | From_Module | From_Ex:
		case Type_Cube | From_Create:
		case Type_Cube | From_File:
		case Type_Cube | From_File | From_Ex:
		case Type_Cube | From_Mem:
		case Type_Cube | From_Ex:
		case Type_Cube | From_Module:
		case Type_Cube | From_Module | From_Ex:
		case Type_Volume | From_Create:
		case Type_Volume | From_File:
		case Type_Volume | From_File | From_Ex:
		case Type_Volume | From_Mem:
		case Type_Volume | From_Ex:
		case Type_Volume | From_Module:
		case Type_Volume | From_Module | From_Ex:
		default:
			myTrace0A(M,"xPict::onCreate() with type(%d) not impliented");
			break;
		} // switch(m_Type)
		if(FAILED(hr)){
			myDx_Trace_hr1(M,hr,"xPict::onCreate() with type(%d) failed",m_Type);
			return XE_APICALL;
		}
		switch(m_Type & Type_Mask)
		{
		case Type_Normal:
			m_Res = m_NormaTexture;
			break;
		case Type_Cube:
			m_Res = m_CubeTexture;
			break;
		case Type_Volume:
			m_Res = m_VolumeTexture;
			break;
		}
		return XE_OK;
	}

	TResult xPict::onLost( xDevice* device )
	{
		return onDelete(device);
	}
	TResult xPict::onReset( xDevice* device )
	{
		if(vNull != m_Res)return XE_OK;
		return onCreate(device);
	}
	TResult xPict::onDelete( xDevice* device )
	{
		SAFE_RELEASE(m_Res);
		return XE_OK;
	}

	xPict& xPict::setSrcFile( Sky::StringPtr& fileName,TBool isConstStatic /*= vFalse*/ )
	{
		m_srcFile = fileName;
		m_Type |= From_File;return *this;
	}

}

